package student.aistar.service.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import student.aistar.mapper.BoxSecKillMapper;
import student.aistar.model.entity.BoxOrder;
import student.aistar.model.entity.BoxSeckill;
import student.aistar.model.entity.Orderdetail;
import student.aistar.model.entity.Orders;
import student.aistar.service.IBoxSeckillService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: zhugefeng
 * @Date:2021/10/14 11:27
 **/
@Service
@Transactional
public class BoxSeckillServiceImpl implements IBoxSeckillService {
    @Autowired
    private BoxSecKillMapper boxSecKillMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<Map<String, Object>> getAllBoxSeckillBefore() {
        return boxSecKillMapper.findAllBoxSeckillBefore();
    }

    @Override
    public List<BoxSeckill> getAllBoxSeckill() {
        return boxSecKillMapper.findAllBoxSeckill();
    }

    @Override
    public List<BoxOrder> getAllBoxOrder() {
        return boxSecKillMapper.findAllBoxOrder();
    }

    @Override
    public void updateBoxState() {
        boxSecKillMapper.updateBoxState();
    }

    @Override
    public List<Long> getBoxById() {
        return boxSecKillMapper.findBoxById();
    }

    @Override
    public void endBoxActive() {
        boxSecKillMapper.endBoxActive();
    }

    @Override
    public List<Long> getBoxEndId() {
        return boxSecKillMapper.findBoxEndId();
    }

    @Override
    public String getBoxStatus(long killId) {
        String status = (String) redisTemplate.opsForValue().get("boxstatus_" + killId);
        if (status == null) {
            status = "2";
        }
        return status;
    }

    @Override
    public void saveBoxOrder(String username, long killid) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("username", username);
        maps.put("killId", killid);

        rabbitTemplate.convertAndSend("boxSeckillExc", "", maps);
    }

    @Override
    public void saveBoxSeckillOrder(String orderNo, String uName, long secId) {
        boxSecKillMapper.saveBoxSeckillOrder(orderNo, uName, secId);
    }

    @Override
    public int insertOrders(Orders order) {
        return boxSecKillMapper.insertOrders(order);
    }

    @Override
    public void insertOrderDetail(Orderdetail orderdetail) {
         boxSecKillMapper.insertOrderDetail(orderdetail);
    }

    @Override
    public BoxSeckill selectBoxSeckillById(Long killId) {
        return boxSecKillMapper.selectBoxSeckillById(killId);
    }
}
